//
//  DJHeaderImageController.swift
//  dj-learn-Swift
//
//  Created by jie sun on 2022/5/22.
//  Copyright © 2022 S.J. All rights reserved.
//

import UIKit
import RxSwift
import MJRefresh

class DJHeaderImageController: DJViewController {

    let navView = DJAutoLayoutNavView()
    
    let headView = DJAutoLayoutHeadView()
    
    let statusView = UIImageView()
    
//    let imageView = UIImageView(image: UIImage(named: "椭圆形备份"))
    let refreshPlaceholerView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.view.backgroundColor = UIColor.lightGray
        
        let bgImage = UIImage.create(with: UIColor.clear)
        self.navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.navigationBar.setBackgroundImage(bgImage, for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage.create(with: UIColor.clear)
        self.navigationItem.leftBarButtonItem = nil
        self.navigationItem.backBarButtonItem = nil
//        self.statusView.backgroundColor = UIColor.init(hex: 0x2B0065, alpha: 1)
        self.statusView.backgroundColor = UIColor.clear
        self.view.addSubview(self.statusView)
        self.statusView.snp.makeConstraints { make in
            make.top.left.right.equalTo(self.view)
            make.height.equalTo(KStatusH)
        }
        
//        self.refreshPlaceholerView.backgroundColor = UIColor.init(hex: 0x2B0065, alpha: 1)
        let image: UIImage! = UIImage(named: "home_head_total")!
        self.refreshPlaceholerView.image = UIImage(named: "home_head_total")
        self.view.addSubview(self.refreshPlaceholerView)
        self.refreshPlaceholerView.frame = CGRect(x: 0, y: 0, width: KScreenWidth, height: KScreenWidth * image.size.height / image.size.width)


        self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsets(top: KStatusH, left: 0, bottom: 0, right: 0))
        }

        // self.navView.backgroundColor = UIColor.init(hex: 0x2B0065, alpha: 1)
        self.navView.backgroundColor = UIColor.clear
        self.view.addSubview(navView)
        self.navView.frame = CGRect(x: 0, y: KStatusH, width: KScreenWidth, height: 44.0)
        
        self.tableView.rx.contentOffset.subscribe(onNext: { offset in
            if offset.y < 0 {
                let image: UIImage! = UIImage(named: "home_head_total")!
                let orH = KScreenWidth * image.size.height / image.size.width
                
                self.refreshPlaceholerView.frame = CGRect(x: 0, y: 0, width: KScreenWidth, height: orH - offset.y)
                self.navView.frame = CGRect(x: 0, y: KStatusH - offset.y, width: KScreenWidth, height: 44.0)
            } else {
                let image: UIImage! = UIImage(named: "home_head_total")!
                let orH = KScreenWidth * image.size.height / image.size.width
                self.refreshPlaceholerView.frame = CGRect(x: 0, y: 0, width: KScreenWidth, height:orH - offset.y)
                
                self.navView.frame = CGRect(x: 0, y: KStatusH, width: KScreenWidth, height: 44.0)
            }
        }).disposed(by: self.disposeBag)
        

//        self.tableView.rx.observe(UIEdgeInsets.self, "contentInset").subscribe(onNext: { contentInset in
//            print("contentInset: \(String(describing: contentInset?.top))")
//        }).disposed(by: self.disposeBag)
    }
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = UIColor.clear
        tableView.delegate = self
        tableView.dataSource = self
        tableView.showsVerticalScrollIndicator = false
        tableView.register(DJAutoLayoutTableCell.self, forCellReuseIdentifier: "DJAutoLayoutTableCell")
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never;
        } else {
            self.automaticallyAdjustsScrollViewInsets = false;
        }
        self.headView.topView.backgroundColor = .clear
        self.headView.frame = CGRect(x: 0, y: 0, width: KScreenW, height: 260.0)
        tableView.tableHeaderView = self.headView
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            let deadline = DispatchTime.now() + 2.0
            DispatchQueue.global().asyncAfter(deadline: deadline) {
                self.tableView.mj_header?.endRefreshing {}
            }
        });
        return tableView
    }()
}

extension DJHeaderImageController : UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DJAutoLayoutTableCell") as! DJAutoLayoutTableCell
        cell.label.text = String.arbitrary()
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return .leastNormalMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return .leastNormalMagnitude
    }
}

